<?php
namespace app\service;
use EasyWeChat\Factory;
use app\service\LogService;
use app\service\OrderService;
use app\common\model\Order as OrderModel;
use app\common\model\ChatMoney as ChatMoneyModel;
use app\common\model\User as UserModel;
use app\common\model\ChatMsg as ChatMsgModel;
use app\common\model\Loginstatus;
use app\common\model\VideoPay as VideoPayModel;
use EasyWeChat\Kernel\Messages\NewsItem;
use think\Db;

class PayResultService extends BaseService
{
  /* 红包发送成功 */
  public static function resultMoney ($moneyLog) {
    $userInfo = UserModel::getOneById($moneyLog['uid']);
    ChatMoneyModel::where('id',$moneyLog['id'])->update(
      ['status' => 1]
    );
    //红包消息
    ChatMsgModel::create([
      'uid' => $moneyLog['uid'],
      'other' => $moneyLog['id'],
      'content' => json_encode([
          'type' => 'message',
          'uid' => $moneyLog['uid'],
          'content' => [
            'content' => $moneyLog['desc'],
            'id' => $moneyLog['id'],
            'type' => 'money',
            'money' => $moneyLog['money'],
            'nickname' => $userInfo['nickname'],
            'uid' => $moneyLog['uid'],
            'status' => true
          ]
      ]),
      'mall_id' => $userInfo['mall_id'],
      'status' => 0,
      'type' => 'money',
      'group_id' => UserModel::getGroupId($moneyLog['uid'])
    ]);

    $obj = WechatService::S([
      'mall_id' => $userInfo['mall_id']
    ]);


      $where['zkhb_loginstatus.update_time']=array("EGT",time()-3600*24);
      //Loginstatus::getAll(,'','uid');
      $recentList = Db::name("user_wechat")
          ->field("zkhb_user_wechat.uid,zkhb_user_wechat.openid")
          ->join("zkhb_loginstatus","zkhb_user_wechat.uid=zkhb_loginstatus.uid")
          ->where($where)
          ->select();

    if($recentList) {
        foreach($recentList as $v) {
            $obj->send(
                2,
                $v['openid'],//UserModel::uid2openid($v['uid']),
                [new NewsItem([
                  'title'       => $userInfo['nickname']."在群内发了一个红包,快来抢红包吧",
                  'description' => '快来抢红包，先到先得',
                  'url'         => request()->domain()."/m/chat/pages/index/index?rid=".$moneyLog['id']."&mall_id={$userInfo['mall_id']}",
                  'image'       => "http://cdn.1s1s.club/shopcom/20190510/1725488242.png",
                ])]
            );
        }
    }
  }
}